package com.brush.web.controller.dictdata;

import com.brush.common.annotation.Log;
import com.brush.common.constant.CacheConstants;
import com.brush.common.core.controller.BaseController;
import com.brush.common.core.domain.R;
import com.brush.common.core.domain.entity.SysDictData;
import com.brush.common.core.domain.resp.SysDictDataQueryResp;
import com.brush.common.enums.BusinessType;
import com.brush.system.domain.req.sysdictdata.SysDictDataAddReq;
import com.brush.system.domain.req.sysdictdata.SysDictDataEditReq;
import com.brush.system.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: SysDictDataController
 * Package: com.brush.web.controller.dictdata
 * Description:
 *
 * @Author luo
 * @Create 2024/8/26 11:04
 * @Version 1.0
 */
@Api(tags = "后台-字典数据api")
@RestController
@RequestMapping(value = "/admin/dict/data")
public class SysDictDataController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysDictDataService sysDictDataService;

    @GetMapping(value = "/type/{dictType}")
    @ApiOperation(value = "根据字典类型查询字典数据")
    public R<List<SysDictDataQueryResp>> findByType(@PathVariable(value = "dictType") String dictType) {
        String redisKey = CacheConstants.SYS_DICT_KEY + dictType;
        List<SysDictDataQueryResp> list = (List<SysDictDataQueryResp>) this.redisTemplate.opsForValue().get(redisKey);
        if (list == null) {
            return R.fail("字典数据" + dictType + "的值不存在");
        }
        return R.ok(list);
    }


    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public R<SysDictDataQueryResp> getInfo(@PathVariable Long dictCode)
    {
        return R.ok(this.sysDictDataService.selectDictDataById(dictCode));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysDictDataAddReq req)
    {
        boolean result=this.sysDictDataService.insertDictData(req);
        return  super.isSuccess(result);
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysDictDataEditReq req) {
        boolean result=this.sysDictDataService.updateDictData(req);
        return  super.isSuccess(result);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes)
    {
        this.sysDictDataService.deleteDictDataByIds(dictCodes);
        return R.ok();
    }
}
